import { IAnswerChecker } from "./IAnswerChecker";
import { IAnswer } from "@/model/Answer";
import { ICheckResponse } from "@/model/CheckResponse";
import { IQuestion } from "@/model/Question";
import { ValidatorResponse } from "@/Types";

import { IValidator } from "@/controlor/Validator/IValidator";
import { RegularExpressionValidator } from "@/controlor/Validator/RegularExpressionValidator";
import { StringNumBetweenValidator } from "@/controlor/Validator/StringNumBetweenValidator";

export class CompareAnsweChecker implements IAnswerChecker {
  Check(question: IQuestion, answer: IAnswer): ICheckResponse {
    let tmpMessage: string;
    let tmpisRight = false;
    const tmpQuestion = Number(question.Question);
    const tmpAnwser = Number(answer.Answer);
    if (tmpAnwser < tmpQuestion) {
      tmpMessage = "输入小了";
    } else if (tmpAnwser > tmpQuestion) {
      tmpMessage = "输入大了";
    } else {
      tmpMessage = "输入对了";
      tmpisRight = true;
    }

    return {
      Message: tmpMessage,
      IsRight: tmpisRight
    };
  }

  Validate(answer: IAnswer): ValidatorResponse[] {
    const rules: IValidator<string>[] = [
      StringNumBetweenValidator(0, 9999, true, true),
      RegularExpressionValidator(`^[0-9]+$`, "输入必须都是数字")
    ];
    return rules.map(validator => {
      return validator(answer.Answer);
    });
  }
}
